package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.ApartmentInfoService;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    ApartmentLabelMapper apartmentLabelMapper;

    @Autowired
    ApartmentFacilityMapper apartmentFacilityMapper;

    @Autowired
    ApartmentFeeValueMapper apartmentFeeValueMapper;

    @Autowired
    GraphInfoMapper graphInfoMapper;

    @Autowired
    ProvinceInfoMapper provinceInfoMapper;

    @Autowired
    DistrictInfoMapper districtInfoMapper;
    @Autowired
    CityInfoMapper cityInfoMapper;

    @Autowired
    FacilityInfoMapper facilityInfoMapper;

    @Autowired
    LabelInfoMapper labelInfoMapper;

    @Autowired
    FeeValueMapper feeValueMapper;

    @Transactional  // 事务
    @Override
    public void saveOrUpdateapartment(ApartmentSubmitVo apartmentSubmitVo) {
        Long apartmentId = apartmentSubmitVo.getId(); // 有就是更新，没有就是保存

        // 封装省市区名称
        Long provinceId = apartmentSubmitVo.getProvinceId();
        Long cityId = apartmentSubmitVo.getCityId();
        Long districtId = apartmentSubmitVo.getDistrictId();
        String provinceName = provinceInfoMapper.selectById(provinceId).getName();
        String cityName = cityInfoMapper.selectById(cityId).getName();
        String districtName = districtInfoMapper.selectById(districtId).getName();
        apartmentSubmitVo.setProvinceName(provinceName);
        apartmentSubmitVo.setCityName(cityName);
        apartmentSubmitVo.setDistrictName(districtName);
        this.saveOrUpdate(apartmentSubmitVo);   // 保存或更新

        // 需要加入到中间表的数据
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();      //  标签
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();    //  设施
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();        //  费用
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();     //  图片

        if (apartmentId != null) {
            // 更新

            // 删除标签中间表数据
            QueryWrapper<ApartmentLabel> apartmentLabelQueryWrapper = new QueryWrapper<>();
            apartmentLabelQueryWrapper.eq("apartment_id", apartmentId);     // 根据apartment_id 删除
            apartmentLabelMapper.delete(apartmentLabelQueryWrapper);

            // 删除配套中间标的数据
            QueryWrapper<ApartmentFacility> apartmentFacilityQueryWrapper = new QueryWrapper<>();
            apartmentLabelQueryWrapper.eq("apartment_id", apartmentId);
            apartmentFacilityMapper.delete(apartmentFacilityQueryWrapper);

            // 删除杂费中间表数据
            QueryWrapper<ApartmentFeeValue> apartmentFeeValueQueryWrapper = new QueryWrapper<>();
            apartmentFeeValueQueryWrapper.eq("apartment_id", apartmentId);
            apartmentFeeValueMapper.delete(apartmentFeeValueQueryWrapper);

            // 删除图片数据
            QueryWrapper<GraphInfo> graphInfoQueryWrapper = new QueryWrapper<>();
            graphInfoQueryWrapper.eq("item_type", ItemType.APARTMENT); // 根据item_type 删除
            graphInfoQueryWrapper.eq("item_id", apartmentId);
            graphInfoMapper.delete(graphInfoQueryWrapper);
        }

        // 插入新数据
        apartmentId = apartmentSubmitVo.getId(); // 避免新增的时候第一次获取id为null

        // 标签中间表
        apartmentLabelMapper.insertBatch(apartmentId, labelIds);


        // 配套中间表
        apartmentFacilityMapper.insertBatch(apartmentId, facilityInfoIds);

        // 杂费中间表
        apartmentFeeValueMapper.insertBatch(apartmentId, feeValueIds);

        // 图片表
        graphInfoMapper.insertBatch(1, apartmentId, graphVoList);
    }

    @Override
    public IPage<ApartmentItemVo> pageItem(IPage<ApartmentItemVo> itemVoIPage, ApartmentQueryVo queryVo) {

        IPage<ApartmentItemVo> iPage = baseMapper.pageItem(itemVoIPage, queryVo);

        return iPage;
    }

    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        ApartmentInfo apartmentInfo = baseMapper.selectById(id);

        // 图片信息
        QueryWrapper<GraphInfo> graphInfoQueryWrapper = new QueryWrapper<>();
        graphInfoQueryWrapper.eq("item_type", ItemType.APARTMENT); //
        graphInfoQueryWrapper.eq("item_id", id);
        List<GraphInfo> graphInfos = graphInfoMapper.selectList(graphInfoQueryWrapper);
        List<GraphVo> graphVos = graphInfos.stream().map(graphInfo -> {
            GraphVo graphVo = new GraphVo();
            BeanUtils.copyProperties(graphInfo, graphVo);
            return graphVo;
        }).collect(Collectors.toList());
//        List<GraphVo> graphVos = new ArrayList<>();
//        for (GraphInfo graphInfo : graphInfos) {
//            GraphVo graphVo = new GraphVo();
//            BeanUtils.copyProperties(graphInfo, graphVo);
//            graphVos.add(graphVo);
//        }  // 这个方法只能处理小数量级  十万条以上只能用流式编程，性能会高些

        // 配套信息(根据ID 获取公寓详细信息)
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByApartmentId(id);

        // 标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByApartmentId(id);

        // 杂费信息
        List<FeeValueVo> feeValueVos = feeValueMapper.selectListByApartmentId(id);

        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();  // 创建对象
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo); // apartmentInfo 拷贝给 apartmentDetailVo
        apartmentDetailVo.setGraphVoList(graphVos); // 图片信息
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);    // 配套信息
        apartmentDetailVo.setLabelInfoList(labelInfoList);  // 标签信息
        apartmentDetailVo.setFeeValueVoList(feeValueVos);
        return apartmentDetailVo;   // 返回
    }
}